package cn.iocoder.yudao.module.wrzj.controller.admin.conversation;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wrzj.controller.admin.conversation.vo.ConversationPageReqVO;
import cn.iocoder.yudao.module.wrzj.controller.admin.conversation.vo.ConversationRespVO;
import cn.iocoder.yudao.module.wrzj.controller.app.conversation.vo.AppConversationPageReqVO;
import cn.iocoder.yudao.module.wrzj.dal.dataobject.conversation.ConversationDO;
import cn.iocoder.yudao.module.wrzj.service.conversation.ConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - chatbase会话记录")
@RestController
@RequestMapping("/wrzj/conversation")
@Validated
public class ConversationController {

    @Resource
    private ConversationService conversationService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除chatbase会话记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wrzj:conversation:delete')")
    public CommonResult<Boolean> deleteConversation(@RequestParam("id") Integer id) {
        conversationService.deleteConversation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得chatbase会话记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wrzj:conversation:query')")
    public CommonResult<ConversationRespVO> getConversation(@RequestParam("id") Integer id) {
        ConversationDO conversation = conversationService.getConversation(id);
        return success(BeanUtils.toBean(conversation, ConversationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得chatbase会话记录分页")
    @PreAuthorize("@ss.hasPermission('wrzj:conversation:query')")
    public CommonResult<PageResult<ConversationRespVO>> getConversationPage(@Valid ConversationPageReqVO pageReqVO) {
        PageResult<ConversationDO> pageResult = conversationService.getConversationPage(BeanUtils.toBean(pageReqVO, AppConversationPageReqVO.class));
        return success(BeanUtils.toBean(pageResult, ConversationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出chatbase会话记录 Excel")
    @PreAuthorize("@ss.hasPermission('wrzj:conversation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConversationExcel(@Valid ConversationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConversationDO> list = conversationService.getConversationPage(BeanUtils.toBean(pageReqVO, AppConversationPageReqVO.class)).getList();
        // 导出 Excel
        ExcelUtils.write(response, "chatbase会话记录.xls", "数据", ConversationRespVO.class,
                        BeanUtils.toBean(list, ConversationRespVO.class));
    }

}
